package com.example.mbl.service.impl;

import com.example.mbl.dao.EventMapper;
import com.example.mbl.dao.IndexMapper;
import com.example.mbl.dao.NewsMapper;
import com.example.mbl.dao.ResearchMapper;
import com.example.mbl.dto.IndexDto;
import com.example.mbl.entity.IndexMotto;
import com.example.mbl.entity.IndexPic;
import com.example.mbl.result.PageResult;
import com.example.mbl.result.WebRestResult;
import com.example.mbl.result.business.IndexResult;
import com.example.mbl.service.IndexService;
import com.example.mbl.util.Tools;
import com.example.mbl.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IndexServiceImpl implements IndexService {
    @Autowired
    private IndexMapper indexMapper;

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private EventMapper eventMapper;

    @Autowired
    private ResearchMapper researchMapper;

    @Override
    public WebRestResult addIndexPic(IndexDto indexDto) {
        WebRestResult result = new WebRestResult();
        indexDto.setId(UUIDUtil.randomUUID());
        if(Tools.isEmpty(indexDto.getFlag()))
            indexDto.setFlag("0");
        if(indexMapper.insertIndexPic(indexDto)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增轮换图失败");
        }
        return result;
    }

    @Override
    public WebRestResult addIndexMotto(IndexDto indexDto) {
        WebRestResult result = new WebRestResult();
        indexDto.setId(UUIDUtil.randomUUID());
        if(Tools.isEmpty(indexDto.getFlag()))
            indexDto.setFlag("0");
        if(indexMapper.insertIndexMotto(indexDto)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增格式失败");
        }
        return result;
    }

    @Override
    public WebRestResult deleteIndexPic(IndexDto indexDto) {
        WebRestResult result = new WebRestResult();
        if(indexMapper.deleteIndexPic(indexDto)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除轮换图失败");
        }
        return result;
    }

    @Override
    public WebRestResult deleteIndexMotto(IndexDto indexDto) {
        WebRestResult result = new WebRestResult();
        if(indexMapper.deleteIndexMotto(indexDto)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除格式失败");
        }
        return result;
    }

    @Override
    public WebRestResult updateIndexPic(IndexDto indexDto) {
        WebRestResult result = new WebRestResult();
        if(indexMapper.updateIndexPic(indexDto)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("更新轮换图失败");
        }
        return result;
    }

    @Override
    public WebRestResult updateIndexMotto(IndexDto indexDto) {
        WebRestResult result = new WebRestResult();
        if(indexMapper.updateIndexMotto(indexDto)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("更新格式失败");
        }
        return result;
    }

    @Override
    public IndexResult getIndex() {
        IndexResult result = new IndexResult();
        result.setIndexPics(indexMapper.getIndexPic());
        result.setIndexMottos(indexMapper.getIndexMotto());
        result.setIndexEvents(eventMapper.getEventList(null,null,null,0,3,null));
        result.setIndexNews(newsMapper.getNewsList(null,null,null,0,3));
        result.setIndexResearchs(researchMapper.getResearchList(null,0,2));
        result.setResult(WebRestResult.SUCCESS);
        return result;
    }

    @Override
    public WebRestResult updateIntroduction(String introduction) {
        WebRestResult result = new WebRestResult();
        if(indexMapper.updateIntroduction(introduction)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("修改实验室介绍失败");
        }
        return result;
    }

    @Override
    public WebRestResult getIntroduction() {
        WebRestResult result = new WebRestResult();
        String introduction = indexMapper.getIntroduction();
        if(!Tools.isEmpty(introduction)){
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage(introduction);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取介绍失败");
        }
        return result;
    }

    @Override
    public PageResult<IndexPic> getIndexPicList() {
        PageResult<IndexPic> result = new PageResult<>();
        List<IndexPic> list = indexMapper.getIndexPic();
        if(list.size()>0){
            result.setRows(list);
        }
        result.setResult(WebRestResult.SUCCESS);
        return result;
    }

    @Override
    public PageResult<IndexMotto> getIndexMottoList() {
        PageResult<IndexMotto> result = new PageResult<>();
        List<IndexMotto> list = indexMapper.getIndexMotto();
        if(list.size()>0){
            result.setRows(list);
        }
        result.setResult(WebRestResult.SUCCESS);
        return result;
    }

    @Override
    public WebRestResult updateEmployInfo(String employinfo) {
        WebRestResult result = new WebRestResult();
        if(indexMapper.updateEmployInfo(employinfo)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else {
            result.setMessage("修改招聘信息失败");
        }
        return result;
    }

    @Override
    public WebRestResult getEmployInfo() {
        WebRestResult result = new WebRestResult();
        result.setMessage(indexMapper.getEmployInfo());
        result.setResult(WebRestResult.SUCCESS);
        return result;
    }
}
